jQuery(function($) {
	$.fn.web_formAways = function(formFields, options) {
		var opts = jQuery.extend({
							loading : "#progress_loading_img",					   		
					   		name : "__f___web_formAways",
					   		action : null,
					   		type : "post",
					   		divId : "web_formAways"					 		
					   }, options);
		var index  = 0;
		return this.each(function() {
			index ++;
			if(opts.action != null) {
				var formId = "me_" + index + "_" + opts.name;
				var htmlForm = buildHTMLForm(formId, opts.type, opts.action, formFields);
				
				// alert(htmlForm);				
				$(this).html(htmlForm);
				$("#" + formId).submit();
			}			
			return this;
		});
	};
	
	/**
	* 
	*/
	$.fn.web_formAjaxAways = function(formFields, options) {
		var opts = jQuery.extend({
							loading : "#progress_loading_img",					   		
					   		name : "__f___web_formAjaxAways",
					   		action : null,
					   		type : "post",
					   		divId : "web_formAways",
					   		callbackPostSubmit : null, //function (responseText, statusText)
					   		dataType : "json"		 		
					   }, options);
		var index  = 0;
		return this.each(function() {
			index ++;
			if(opts.action != null) {
				var formId = "me_" + index + "_" + opts.name;
				var htmlForm = buildHTMLForm(formId, opts.type, opts.action, formFields);
				// alert(htmlForm);
				$(this).html(htmlForm);
				
				var ajaxOption = {
					beforeSubmit: function(formData, jqForm, options) { // pre-submit callback
						//$.web_message.clear();
						
						//+++ show loading image
						if(opts.loading !== null && opts.loading !== 'undefined') {
							//$(opts.loading).web_showLoading();
						}
						//---
						
					    // here we could return false to prevent the form from being submitted; 
					    // returning anything other than false will allow the form submit to continue 
					    return true;
					},  
			        success: function (responseText, statusText) { // post-submit callback    
		        		try {
		        			//+++ hidden loading image
		        			if(opts.loading !== null && opts.loading !== 'undefined') {
								//$(opts.loading).web_hiddenLoading();
							}
							//---
		        			if(opts.callbackPostSubmit !== null && $.isFunction(opts.callbackPostSubmit)) {
		        				opts.callbackPostSubmit(responseText, statusText);
		        			}
		        	 	} catch (e) {}
		        	},       			 			         
			        dataType:  opts.dataType        // 'xml', 'script', or 'json' (expected server response type)        
			        // $.ajax options can be used here too, for example: 
			        //timeout:   3000 
				};
				
				$("#" + formId).ajaxSubmit(ajaxOption);
			}			
			return this;
		});
	};
	
	/**
	* @param formId: string
	* @param type: post | get
	* @param action (htm file)
	* @param formFields = {
	*		field_1 : field_value_1,
	*		field_2 : field_value_2,
	* };
	* @return htmlForm 
	*/
	function buildHTMLForm(formId, type, action, formFields) {
		var htmlForm = '<form id="' + formId + '" name="' + formId + '" method="' + type + '" action="' + action + '">';						
		for(var fieldName in formFields) {
			var fieldValue = formFields[fieldName];
			var type = typeOf(fieldValue);
			// alert("type:" + type);
			if(type !== 'function' || type != null || type != 'undefined') {
				if (type == 'array') {
					// alert("array - fieldName:" + fieldName);
					for(var index in fieldValue) {
						htmlForm += '<input type="hidden" name="' + fieldName + '" value="' +fieldValue[index]+ '"/>';
					}
				} else {
					htmlForm += '<input type="hidden" name="' + fieldName + '" value="' +fieldValue+ '"/>';
				}
			} 
		}		
		htmlForm += '</form>';
		return htmlForm;
	};
	
	/**
	* typeOf return type of value
	*
	* Object  	 'object'
	* Array 	'array'
	* Function 	'function'
	* String 	'string'
	* Number 	'number'
	* Boolean 	'boolean'
	* null 		'null'
	* undefined 	'undefined'
	*/
	function typeOf(value) {
	    var s = typeof value;
	    if (s === 'object') {
	        if (value) {
	            if (value instanceof Array || value.length) {
	                s = 'array';
	            }
	        } else {
	            s = 'null';
	        }
	    }
	    return s;
	}
});

/**
* static function
* jQuery.web_formAways.nav({"field_1" : "field_value_1", "field_2" : "field_value_2"}, {action : "url..."})
*/
jQuery.web_formAways = {
	nav : function(formFields, options) {
		try {
			var opts = jQuery.extend({	
					loading : "#progress_loading_img",				   		
			   		name : "__f___web_formAways",
			   		action : null,
			   		type : "post",
			   		divId : "web_formAways"					 		
			   }, options);
			 jQuery("#" + opts.divId).web_formAways(formFields, opts);
		 } catch (e) {
		 	alert("$.web_formAways:: " + e);
		 }
		},
		
	ajaxNav : function(formFields, options) {
		try {
			var opts = jQuery.extend({
					loading : "#progress_loading_img",					   		
			   		name : "__f___web_formAjaxAways",
			   		action : null,
			   		type : "post",
			   		divId : "web_formAways",
			   		callbackPostSubmit : null, //function (responseText, statusText)
			   		dataType : "json"					 		
			   }, options);			
			jQuery("#" + opts.divId).web_formAjaxAways(formFields, opts);
		 } catch (e) {
		 	alert("$.web_formAways:: " + e);
		 }
		}
};